﻿var pics = "";
var dataYw = "";
$(function () {
    loadYw();    
    loadDepartment();    
    uploadPic();

    //取消弹出对话框
    $('#dialogs').on('click', '.weui-dialog__btn_default', function () {
        $(this).parents('.js_dialog').fadeOut(200);
    });
    //跳出提示对话框
    $("#AddDialogs").click(function () {
        var $androidDialog1 = $('#androidDialog1');
        $androidDialog1.fadeIn(200);
    });

    $("#btnSave").click(function () {
        if (validate()){
            save();
        //清空表单
        $("#YwId").val(999);
        $("#DepartmentId").val(999);
        $("#Name").val("");
        $("#IdNumber").val("");
        $("#Tel").val("");
        $("#Sqly").val("");
        $("#Pics").val("");
        }
    });

});

//保存
function save() {
    var postData = {
        "Id": "0",
        "YwId": $("#YwId").val(),
        "DepartmentId": $("#DepartmentId").val(),
        "Name": $("#Name").val(),
        "IdNumber": $("#IdNumber").val(),
        "Tel": $("#Tel").val(),
        "Sqly": $("#Sqly").val(),
        "Pics": pics,
        "PublicTime": GetTime()
    };    
    $.ajax({
        type: "Post",
        url: "/ZLYJ/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {

                var $toast = $('#toast');
                if ($toast.css('display') != 'none') return;
                $toast.fadeIn(50);
                setTimeout(function () {
                    $toast.fadeOut(100);
                }, 2000);

            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};

//此处加载所有派出所
function loadDepartment() {
    $("#DepartmentId").html();
    var option = "";

    $.ajax({
        type: "GET",
        url: "/Sys/Department/GetAllDepIsDisplay?_t=" + new Date().getTime(),        
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            $("#DepartmentId").append(option);
        }
    });
}

//此处加载所有可以前台显示的业务
function loadYw() {
    $("#YwId").html();
    var option = "";
    async: false,
    $.ajax({
        type: "GET",
        url: "/Yw/GetAllYwIsClys?_t=" + new Date().getTime(),

        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            $("#YwId").append(option);
            dataYw = data;
        }
    });
}

function changeYw(YwId) {
    $("#yw_ApplicationMaterials").html("");
    var yw=$("#yw_ApplicationMaterials");
    if (YwId == 999) {
        yw.val("");
    }
    for (var i = 0; i < dataYw.length; i++) {       
        if (dataYw[i].id == YwId) {
            $("#yw_ApplicationMaterials").html(dataYw[i].applicationMaterials);
        }
    }

}
//图片上传
function uploadPic() {    
    $("#uploaderInput").on("change", function () {
        lrz(this.files[0], { width: 800 })
            .then(function (rst) {
                rst.formData.append('fileLen', rst.fileLen);
                $.ajax({
                    type: "POST",
                    url: "/Picture/UploadFilesAjax",
                    contentType: false,
                    processData: false,
                    data: rst.formData,
                    success: function (item) {
                        //alert("上传成功！");
                        pics = pics + "|" + item;
                        $("#Pics").val(pics);

                        // $("#img").html("<img src='/image/" + $('#PicPath').val() + "' style='width:100px;'>");
                        var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>';
                        $("#uploaderFiles").append($(tmpl.replace('#url#', '/UploadFiles/' + item)));
                    },
                    error: function () {
                        alert("上传失败!");
                    }
                });
            })
            .catch(function (err) {
                // 处理失败会执行
            })
            .always(function () {
                // 不管是成功失败，都会执行
            });
    });
}


function GetTime(date) {
    if (date == null) {
        date = new Date();
    }
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    //var S = date.getTime() % 1000;
    var html = y + "-";
    if (M < 10) {
        html += "0";
    }
    html += M + "-";

    if (d < 10) {
        html += "0";
    }
    html += d + " ";
    if (h < 10) {
        html += "0";
    }
    html += h + ":";
    if (m < 10) {
        html += "0";
    }
    html += m + ":";
    if (s < 10) {
        html += "0";
    }
    html += s;
    //html += " ";
    //if (S < 100) {
    //    html += "0"
    //}
    //if (S < 10) {
    //    html += "0";
    //}
    //html += S;
    return html;
}

//验证表单
function validate() {
    var ywId = $("#YwId").val();
    if (ywId == 999) {
        str = '部门没有选择';
        layer.tips(str, "#btnSave");
        $("#YwId").focus();
        return false;
    }

    var dpartmentId = $("#DepartmentId").val();
    if (dpartmentId == 999) {
        str = '部门没有选择';
        layer.tips(str, "#btnSave");
        $("#DepartmentId").focus();
        return false;
    }

    var name = $("#Name").val();
    if ($.trim(name).length == 0) {
        str = '姓名没有输入';
        layer.tips(str, "#btnSave");
        $("#Name").focus();
        return false;
    } else {
        if (isChinaName(name) == false) {
            str = '请输入中文姓名';
            layer.tips(str, "#btnSave");
            $("#Name").focus();
            return false;
        }
    }

    var iDNumber = $("#IdNumber").val();
    if ($.trim(iDNumber).length == 0) {
        str = '身份证号码没有输入';
        layer.tips(str, "#btnSave");
        $("#IdNumber").focus();
        return false;
    } else {
        if (isCardNo(iDNumber) == false) {
            str = '身份证号不正确';
            layer.tips(str, "#btnSave");
            $("#IdNumber").focus();
            return false;
        }
    }

    var tel = $("#Tel").val();
    if ($.trim(tel).length < 6) {
        str = '联系电话没有输入';
        layer.tips(str, "#btnSave");
        $("#Tel").focus();
        return false;
    }

    var sqly = $("#Sqly").val();
    if ($.trim(sqly).length == 0) {
        str = '申请理由没有输入';
        layer.tips(str, "#btnSave");
        $("#Sqly").focus();
        return false;
    }

    var pics = $("#Pics").val();
    if ($.trim(pics).length == 0) {
        str = '请至少上传一张图片';
        layer.tips(str, "#btnSave");
        $("#Pics").focus();
        return false;
    }

    return true;
}

// 验证中文名称
function isChinaName(name) {
    var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
    return pattern.test(name);
}

// 验证身份证长度
function isCardNo(card) {
    var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return pattern.test(card);
}